﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using VocabManager.Model;
using System.Linq;

namespace VocabManager.DataLayer
{
    public class ItemRepository
    {
        FlashCardContext _dataContext;
        public ItemRepository(FlashCardContext dataContext)
        {
            _dataContext = dataContext;
        }
        public List<Item> GetAllItems(int deckId)
        {
            return _dataContext.Cards.Where(item => item.DeckId == deckId).Select(item =>
               BuildItemFromCardData(item)).ToList();
        }

        private VocabManager.Model.Item BuildItemFromCardData(Card card)
        {
            VocabManager.Model.BackPart back = new BackPart() { Text = card.BackText, BackgroundBrush = card.BackBrush, TextBrush = card.BackTextBrush, Sound=card.BackSound };
            VocabManager.Model.FrontPart front = new FrontPart() { Text = card.FrontText, BackgroundBrush = card.FrontBrush, TextBrush = card.FrontTextBrush, Sound= card.FrontSound };
            
            VocabManager.Model.Item item = new Item(front, back);
            item.Id = card.Id;
            item.LastModificationTime = card.LastModifiedTime;
            item.CreationTime = card.CreationTime;

            return item;

        }

        internal void RemoveItem(Item item)
        {
            Card dataCard = _dataContext.Cards.FirstOrDefault(i => item.Id == i.Id);
            _dataContext.Cards.DeleteOnSubmit(dataCard);
            _dataContext.SubmitChanges();
        }

        internal void AddNewItem(Item item)
        {
            Card dataCard = new Card()
            {
                CreationTime = DateTime.Now,
                LastModifiedTime = DateTime.Now,
                FrontText = item.Front.Text,
                BackText = item.Back.Text,
                DeckId = item.DeckId,
                BackTextBrush = item.Back.TextBrush,
                FrontTextBrush = item.Front.TextBrush,
                BackBrush = item.Back.BackgroundBrush,
                FrontBrush = item.Front.BackgroundBrush,
                FrontImage = item.Front.Image,
                FrontSound = item.Front.Sound,
                BackImage = item.Back.Image,
                BackSound = item.Back.Sound

            };
            _dataContext.Cards.InsertOnSubmit(dataCard);
            _dataContext.SubmitChanges();
            item.Id = dataCard.Id;
            item.CreationTime = dataCard.CreationTime;
            item.LastModificationTime = dataCard.LastModifiedTime;
        }

        internal void UpdateItem(Item item)
        {
            Card dataCard = _dataContext.Cards.FirstOrDefault(i => item.Id == i.Id);
            AssignItemToDataCard(dataCard, item);
            _dataContext.SubmitChanges();
        }

        private void AssignItemToDataCard(Card dataCard, Item item)
        {
            dataCard.FrontText = item.Front.Text;
            dataCard.BackText = item.Back.Text;
            dataCard.BackTextBrush = item.Back.TextBrush;
            dataCard.FrontTextBrush = item.Front.TextBrush;
            dataCard.BackBrush = item.Back.BackgroundBrush;
            dataCard.FrontBrush = item.Front.BackgroundBrush;
            dataCard.FrontImage = item.Front.Image;
            dataCard.FrontSound = item.Front.Sound;
            dataCard.BackImage = item.Back.Image;
            dataCard.BackSound = item.Back.Sound;
        }

    }
}
